import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

public class Demo7 {
    public static void main(String[] args) throws IOException {
        /*InputStream inputStream = null;
        try {
            inputStream = new FileInputStream("./test.txt");
        } finally {
            inputStream.close();
        }*/

        //上述代码不规范，应该使用try-with-resources语法
        try (InputStream inputStream = new FileInputStream("./test.txt")/*;可以定义多个资源，用分号隔开
             InputStream inputStream2 = new FileInputStream("./test2.txt")*/) {//出了try块，资源会自动释放，相当于自动调用inputStream.close()

            //读文件操作
            while (true) {
                /*int date = inputStream.read();//不带参数表示读取一个字节
                if (date == -1) {
                    //读完文件
                    break;
                }
                System.out.printf("0x%x\n", date);*/

                //一次读取多个字节,数组长度进行自定义
                byte[] bytes = new byte[3];
                //读操作，就会尽可能把字节数组给填满
                //填不满的话，能填几个就是几个
                int len = inputStream.read(bytes);
                System.out.println("len= " + len);
                //len就是表示实际读取了几个字节
                if (len == -1) {
                    //读完文件
                    break;
                }

                for (int i = 0; i < len; i++) {
                    System.out.printf("0x%x \n", bytes[i]);//打印每个字节的十六进制数
                }

                System.out.println("===========");
            }

        }
    }
}
